package com.bookmarket.controller;

import com.bookmarket.entity.Active;
import com.bookmarket.entity.Btype;
import com.bookmarket.service.HJHActiveService;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/active")
public class HJHActiveController {


    @Resource
    private HJHActiveService hJHActiveService;

    @RequestMapping("/findAll")
    public Map findAll(int pageNum, int pageSize, Active active){
        Map map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("active", active);
        Page page =hJHActiveService.findAll(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());
        return map;
    }

    @RequestMapping("/tiao")
    public ModelAndView tiao(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("backstage/HJHAddActive");
        return mv;
    }

   /* @RequestMapping("/tiao1")
    public ModelAndView tiao1(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("backstage/HJHActive");
        return mv;
    }*/

    @RequestMapping("/delActive")
    public ModelAndView delActive(int id){
        ModelAndView mv=new ModelAndView();
        hJHActiveService.delActive(id);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

    @RequestMapping("/findActive")
    public ModelAndView findActive(int id){
        ModelAndView mv=new ModelAndView();
        Active active=hJHActiveService.findActive(id);
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("active",active);
        return mv;
    }

    @RequestMapping("/findBtype")
    public ModelAndView findBtype(){
        ModelAndView mv=new ModelAndView();
        List<Btype> list=hJHActiveService.findBtype();
        mv.addObject("btype",list);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

    @RequestMapping("/addActive")
    public ModelAndView addActive(MultipartFile file, Active active,int bid,HttpSession session) {

        //创建文件保存路径
        String path = session.getServletContext().getRealPath("/uploads");
        Map<String,Object> map=new HashMap();
        ModelAndView mv=new ModelAndView();
        //获取原始文件的名称
        String fileName=file.getOriginalFilename();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


        file.getContentType();//文件类型

        //System.out.println(fileName);
        //目标文件
       File dest=new File(path,fileName);
        try {
            //保存文件
            file.transferTo(dest);
        }catch (IOException e){
            e.printStackTrace();
        }
        Btype btype=new Btype();
        btype.setId(bid);
        active.setBtype(btype);
        active.setPicture(fileName);

        map.put("active",active);
        hJHActiveService.addActive(map);

        //mv.setView(new MappingJackson2JsonView());
        //mv.addObject("AAA","AAA");
        mv.setViewName("backstage/HJHActive");

        return mv;
    }

    @RequestMapping("/updateActive")
    public ModelAndView updateActive(MultipartFile file, Active active,int bid,HttpSession session) {

        //创建文件保存路径
        String path = session.getServletContext().getRealPath("/uploads");
        Map<String,Object> map=new HashMap();
        ModelAndView mv=new ModelAndView();
        //获取原始文件的名称
        String fileName=file.getOriginalFilename();
        file.getContentType();//文件类型

        //System.out.println(fileName);
        //目标文件
        File dest=new File(path,fileName);
        try {
            //保存文件
            file.transferTo(dest);
        }catch (IOException e){
            e.printStackTrace();
        }
        Btype btype=new Btype();
        btype.setId(bid);
        active.setBtype(btype);
        active.setPicture(fileName);

        map.put("active",active);

        System.out.println(active.getId());
        hJHActiveService.updateActive(map);
        mv.setViewName("backstage/HJHActive");
        return mv;
    }

    //上面是后台部分，下面是前台部分

    //首页获取活动图片
/*    @RequestMapping("GetActivePicture")
    public ModelAndView GetActivePicture(){
        ModelAndView mv=new ModelAndView();

        mv.setView(new MappingJackson2JsonView());
        return mv;
    }*/

    //跳至retive方法
    @RequestMapping("/toActive")
    public ModelAndView linkToActive(){
        ModelAndView mv=new ModelAndView();
        List<Active> list=hJHActiveService.GetActivePicture();
        mv.addObject("ActivePictures",list);
      /*  for (Active a:list) {
            System.out.println(a.getPicture()+"::::::"+a.getContent());
        }*/
        mv.setViewName("stage/LQ_active");
        return mv;
    }

    @RequestMapping("/target")
    public ModelAndView target(int id){
        ModelAndView mv=new ModelAndView();
        //System.out.println(id);
        Active active=hJHActiveService.GetPicture(id);
        //System.out.println(active.getPicture());
        //mv.addObject("picture",active.getPicture());
        mv.addObject("Active",active);
        //System.out.println(active.getPicture());
        mv.setViewName("stage/HJHStageActive");
        return mv;
    }

    @RequestMapping("/ActiveBook")
    public Map ActiveBook(int pageNum, int pageSize, int id){
        Map map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("id", id);
        Page page =hJHActiveService.findAllBook(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());
        //System.out.println(page.get(0));
        return map;
    }

    public HJHActiveService gethJHActiveService() {
        return hJHActiveService;
    }

    public void sethJHActiveService(HJHActiveService hJHActiveService) {
        this.hJHActiveService = hJHActiveService;
    }
}